/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_title.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_pwtitle_html_file(GraphFrame *gf,FILE *fp);
void
write_body_ord_pwtitle_html_file(GraphFrame *gf,FILE *fp);

void
write_body_orphan_html_file(GraphFrame *gf,FILE *fp);
void
write_body_ord_orphan_html_file(GraphFrame *gf,FILE *fp);

void
write_body_img_no_alt_html_file(GraphFrame *gf,FILE *fp);
void
write_body_pages_img_no_alt_html_file(GraphFrame *gf,FILE *fp);

void
write_body_ord_img_no_alt_html_file(GraphFrame *gf,FILE *fp);
void
write_body_ord_pages_img_no_alt_html_file(GraphFrame *gf,FILE *fp);

int 
report_pages_without_title(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Pages without title");
  write_body_pwtitle_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_pwtitle_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Pages without Title");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPWTString);
  write_horizontal_rule_html_file(fp);
  write_start_ol_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      char *la = get_label_vertex(v);
      const char *att = NULL;

      if(get_shape(v) != BOX)
	  continue;

      att = get_attribute(v,"title");

      if(att && !strcmp(att,"No title"))
      {
	write_item_html_file(fp);
	write_start_a_href_html_file(fp,la);
	fprintf(fp,"%s",la);
	write_end_a_href_html_file(fp);
      }
    }
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
}

int 
report_ord_pages_without_title(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Pages without title");
  write_body_ord_pwtitle_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_ord_pwtitle_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;

  int nptit = 0;
  PNODE *varray = NULL;
  int i = 0;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Pages without Title");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPWTString);
  write_horizontal_rule_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      const char *att = NULL;

      if(get_shape(v) != BOX)
	  continue;

      att = get_attribute(v,"title");

      if(att && !strcmp(att,"No title"))
	nptit++;
    }

  if(nptit == 0)
  {
    fprintf(fp, "No Pages without title\n");
    write_end_body_html_file(fp);
    return;
  }
  varray = nptit ? (PNODE *)calloc(nptit,sizeof(PNODE)) : NULL;

  if(!varray) {
  fprintf(stderr,"Out of Memory write_body_ord_pwtitle_html_file\n");
      abort();
  }
  i=0;

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      const char *att = NULL;

      if(get_shape(v) != BOX)
	  continue;

      att = get_attribute(v,"title");

      if(att && !strcmp(att,"No title"))
	  varray[i++] = v;
    }

  randomized_quicksort((GPTR *)varray,0,nptit-1,
		       is_vertex_label_lower,is_vertex_label_bigger);

  write_start_ol_html_file(fp);

  for(i=0;i<nptit; i++)
  {
    PNODE v = varray[i];
    char *la = get_label_vertex(v);
    write_item_html_file(fp);
    write_start_a_href_html_file(fp,la);
    fprintf(fp,"%s",la);
    write_end_a_href_html_file(fp);
  }

  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
  free(varray); varray = NULL;
}

int 
report_orphan_pages(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Orphan Pages");
  write_body_orphan_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_orphan_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Orphan Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,ROPString);
  write_horizontal_rule_html_file(fp);
  write_start_ol_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      char *la = get_label_vertex(v);
      const char *att = NULL;
      LNode_type *ledges_out = get_edges_out(v);
      LNode_type *pei = NULL, *ledges_in = NULL; 

      if(get_shape(v) != BOX)
	  continue;

      if(!is_empty_list(ledges_out))
	  continue;

      write_item_html_file(fp);

      att = get_attribute(v,"title");

      if(att)
      {
	write_start_a_href_html_file(fp,la);
	fprintf(fp,"%s",att);
	write_break_html_file(fp);
	fprintf(fp,"%s",la);
	write_end_a_href_html_file(fp);
      }
      else
      {
	write_item_html_file(fp);
	write_start_a_href_html_file(fp,la);
	fprintf(fp,"%s",la);
	write_end_a_href_html_file(fp);
      }

      ledges_in = get_edges_in(v);
      write_start_ul_html_file(fp);

      for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
	{
	  PEDGE e = (PEDGE)pei->info;
	  PNODE u = get_from_vertex(e);

	  if(get_edge_attribute(e) == 'z')
	    continue;

	  {
	    char *sanchor = get_label_edge(e);
	    const char *att = get_attribute(u,"title");
	    char *la = get_label_vertex(u);

	    write_item_html_file(fp);
	    if(sanchor)	
	    {
	      write_start_pre_html_file(fp);
	      write_bold_html_file(fp,"Caption:");
	      write_string_html_file(fp,sanchor);
	      write_end_pre_html_file(fp);
	    }
	    if(att)
	    {
	      write_start_a_href_html_file(fp,la);
	      fprintf(fp,"%s",att);
	      write_break_html_file(fp);
	      fprintf(fp,"%s",la);
	      write_end_a_href_html_file(fp);
	    }
	    else
	    {
	      write_start_a_href_html_file(fp,la);
	      fprintf(fp,"%s",la);
	      write_end_a_href_html_file(fp);
	    }

	  }
	}
      write_end_ul_html_file(fp);	
    }
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
}

int 
report_ord_orphan_pages(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Orphan Pages");
  write_body_ord_orphan_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_ord_orphan_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;

  int norp = 0;
  PNODE *varray = NULL;
  int i = 0;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Orphan Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,ROPString);
  write_horizontal_rule_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *ledges_out = get_edges_out(v);

      if(get_shape(v) != BOX)
	  continue;

      if(is_empty_list(ledges_out))
	  norp++;
    }

  if(norp == 0)
  {
    fprintf(fp, "No Orphan Pages \n");
    write_end_body_html_file(fp);
    return;
  }

  varray = norp ? (PNODE *)calloc(norp,sizeof(PNODE)) : NULL;

  if(!varray) {
  fprintf(stderr,"Out of Memory write_body_ord_orphan_html_file\n");
      abort();
  }
  i=0;

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *ledges_out = get_edges_out(v);

      if(get_shape(v) != BOX)
	  continue;

      if(is_empty_list(ledges_out))
	varray[i++] = v;
    }

  randomized_quicksort((GPTR *)varray,0,norp-1,
		       is_vertex_label_lower,is_vertex_label_bigger);

  write_start_ol_html_file(fp);

  for(i=0;i<norp; i++)
  {
    PNODE v = varray[i];
    char *la = get_label_vertex(v);
    const char *att = NULL;
    LNode_type *pei = NULL, *ledges_in = NULL; 

    write_item_html_file(fp);

    att = get_attribute(v,"title");

    if(att)
    {
      write_start_a_href_html_file(fp,la);
      fprintf(fp,"%s",att);
      write_break_html_file(fp);
      fprintf(fp,"%s",la);
      write_end_a_href_html_file(fp);
    }
    else
    {
      write_item_html_file(fp);
      write_start_a_href_html_file(fp,la);
      fprintf(fp,"%s",la);
      write_end_a_href_html_file(fp);
    }

    ledges_in = get_edges_in(v);
    write_start_ul_html_file(fp);
    
    for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
    {
      PEDGE e = (PEDGE)pei->info;
      PNODE u = get_from_vertex(e);

      if(get_edge_attribute(e) == 'z')
	  continue;

      {
	  char *sanchor = get_label_edge(e);
	  const char *att = get_attribute(u,"title");
	  char *la = get_label_vertex(u);
	  
	  write_item_html_file(fp);
	  if(sanchor)	
	  {
	    write_start_pre_html_file(fp);
	    write_bold_html_file(fp,"Caption:");
	    write_string_html_file(fp,sanchor);
	    write_end_pre_html_file(fp);
	  }
	  if(att)
	  {
	    write_start_a_href_html_file(fp,la);
	    fprintf(fp,"%s",att);
	    write_break_html_file(fp);
	    fprintf(fp,"%s",la);
	    write_end_a_href_html_file(fp);
	  }
	  else
	  {
	    write_start_a_href_html_file(fp,la);
	    fprintf(fp,"%s",la);
	    write_end_a_href_html_file(fp);
	  }
	  
      }
    }
    write_end_ul_html_file(fp);	
  }
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
  free(varray); varray = NULL;
}



int 
report_images_no_alt(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"List of Images without ALT");
  write_body_img_no_alt_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_img_no_alt_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodesobj = get_list_objects(gf);
  LNode_type *ptr = nodesobj->back;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Images without ALT");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPIAString);
  write_horizontal_rule_html_file(fp);
  write_start_ol_html_file(fp);

  for(; ptr != nodesobj; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *pei = NULL, *ledges_in = NULL; 
      int found = 0;

      ledges_in = get_edges_in(v);


      for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
	{
	  PEDGE e = (PEDGE)pei->info;
	  PNODE u = get_from_vertex(e);
	  char *sanchor = get_label_edge(e);
	  const char *att = get_attribute(u,"title");
	  char *la = get_label_vertex(u);
	  
	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;

	  found++;
	  if(found == 1)
	  {
	      write_item_html_file(fp);
	      write_start_pre_html_file(fp);
	      write_string_html_file(fp,get_label_vertex(v));
	      write_end_pre_html_file(fp);
	      write_start_ul_html_file(fp);
	  }

	  write_item_html_file(fp);
	
	  if(att)
	  {
	    write_start_a_href_html_file(fp,la);
	    fprintf(fp,"%s",att);
	    write_break_html_file(fp);
	    fprintf(fp,"%s",la);
	    write_end_a_href_html_file(fp);
	  }
	  else
	  {
	    write_start_a_href_html_file(fp,la);
	    fprintf(fp,"%s",la);
	    write_end_a_href_html_file(fp);
	  }
	}
      if(found != 0)
	  write_end_ul_html_file(fp);	
    }
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
}


int 
report_ord_images_no_alt(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"List of Images without ALT");
  write_body_ord_img_no_alt_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_ord_img_no_alt_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodesobj = get_list_objects(gf);
  LNode_type *ptr = nodesobj->back;

  int nimg = 0;
  PNODE *varray = NULL;
  int i = 0;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Images without ALT");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPIAString);
  write_horizontal_rule_html_file(fp);

  for(; ptr != nodesobj; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *pei = NULL, *ledges_in = NULL; 

      ledges_in = get_edges_in(v);

      for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
	{
	  PEDGE e = (PEDGE)pei->info;
	  char *sanchor = get_label_edge(e);
	  
	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;
	  
	  nimg++;
	  break;
	}
    }

  if(nimg == 0)
  {
    fprintf(fp, "No Images without alt tag\n");
    write_end_body_html_file(fp);
    return;
  }

  varray = nimg ? (PNODE *)calloc(nimg,sizeof(PNODE)) : NULL;

  if(!varray) {
  fprintf(stderr,"Out of Memory write_body_ord_img_no_alt_html_file\n");
      abort();
  }
  i=0;
  for(ptr = nodesobj->back; ptr != nodesobj; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *pei = NULL, *ledges_in = NULL; 

      ledges_in = get_edges_in(v);

      for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
	{
	  PEDGE e = (PEDGE)pei->info;
	  char *sanchor = get_label_edge(e);
	  
	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;
	  
	  varray[i++] = v;
	  break;
	}
    }

  randomized_quicksort((GPTR *)varray,0,nimg-1,
		       is_vertex_label_lower,is_vertex_label_bigger);

  write_start_ol_html_file(fp);

  for(i=0;i<nimg; i++)
  {
    PNODE v = varray[i];
    LNode_type *pei = NULL, *ledges_in = NULL; 
    int found = 0;

    ledges_in = get_edges_in(v);

    for(pei = ledges_in->back ; pei != ledges_in ; pei = pei->back)
    {
      PEDGE e = (PEDGE)pei->info;
      PNODE u = get_from_vertex(e);
      char *sanchor = get_label_edge(e);
      const char *att = get_attribute(u,"title");
      char *la = get_label_vertex(u);
      
      if(!sanchor)
	 continue;

      if(sanchor && sanchor[0] !='S')
	  continue;

      found++;
      if(found == 1)
      {
	write_item_html_file(fp);
	write_start_pre_html_file(fp);
	write_string_html_file(fp,get_label_vertex(v));
	write_end_pre_html_file(fp);
	write_start_ul_html_file(fp);
      }

      write_item_html_file(fp);
	
      if(att)
      {
	write_start_a_href_html_file(fp,la);
	fprintf(fp,"%s",att);
	write_break_html_file(fp);
	fprintf(fp,"%s",la);
	write_end_a_href_html_file(fp);
      }
      else
      {
	write_start_a_href_html_file(fp,la);
	fprintf(fp,"%s",la);
	write_end_a_href_html_file(fp);
      }
    }
    if(found != 0)
      write_end_ul_html_file(fp);	
  }	
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
  free(varray); varray = NULL;
}


int 
report_pages_images_no_alt(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"List of Pages with Images without ALT");
  write_body_pages_img_no_alt_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_pages_img_no_alt_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Pages with Images without ALT");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPIAString);
  write_horizontal_rule_html_file(fp);
  write_start_ol_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      int found = 0;

      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  PNODE u = get_to_vertex(e);
	  char *sanchor = get_label_edge(e);
	  const char *att = get_attribute(v,"title");
	  char *la = get_label_vertex(v);

	  if(get_edge_attribute(e) != 'z')
	    continue;

	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;

	  found++;
	  if(found == 1)
	  {
	    write_item_html_file(fp);
	    if(att)
	    {
	      write_start_a_href_html_file(fp,la);
	      fprintf(fp,"%s",att);
	      write_break_html_file(fp);
	      fprintf(fp,"%s",la);
	      write_end_a_href_html_file(fp);
	    }
	    else
	    {
	      write_start_a_href_html_file(fp,la);
	      fprintf(fp,"%s",la);
	      write_end_a_href_html_file(fp);
	    }
	    write_start_ul_html_file(fp);
	  }
	  write_item_html_file(fp);
	  write_start_pre_html_file(fp);
	  write_string_html_file(fp,get_label_vertex(u));
	  write_end_pre_html_file(fp);
	}
      if(found != 0)
	  write_end_ul_html_file(fp);	
    }
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
}

int 
report_ord_pages_images_no_alt(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"List of Pages with Images without ALT");
  write_body_ord_pages_img_no_alt_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_ord_pages_img_no_alt_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;

  int npimg = 0;
  PNODE *varray = NULL;
  int i = 0;

  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  write_header_html_file(fp,1,"List of Pages with Images without ALT");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPIAString);
  write_horizontal_rule_html_file(fp);

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);

      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  char *sanchor = get_label_edge(e);

	  if(get_edge_attribute(e) != 'z')
	    continue;

	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;

	  npimg++;
	  break;
	}
    }

  if(npimg == 0)
  {
    fprintf(fp, "No Images without alt tag\n");
    write_end_body_html_file(fp);
    return;
  }
  varray = npimg ? (PNODE *)calloc(npimg,sizeof(PNODE)) : NULL;

  if(!varray) {
  fprintf(stderr,"Out of Memory write_body_ord_pages_img_no_alt_html_file\n");
      abort();
  }
  i=0;
  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);

      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  char *sanchor = get_label_edge(e);

	  if(get_edge_attribute(e) != 'z')
	    continue;

	  if(!sanchor)
	      continue;

	  if(sanchor && sanchor[0] !='S')
	      continue;

	  varray[i++] = v;
	  break;
	}
    }
  randomized_quicksort((GPTR *)varray,0,npimg-1,
		       is_vertex_label_lower,is_vertex_label_bigger);

  write_start_ol_html_file(fp);

  for(i=0;i<npimg; i++)
  {
    PNODE v = varray[i];
    LNode_type *peo, *ledges_out = get_edges_out(v);
    int found = 0;

    for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
    {
      PEDGE e = (PEDGE)peo->info;
      PNODE u = get_to_vertex(e);
      char *sanchor = get_label_edge(e);
      const char *att = get_attribute(v,"title");
      char *la = get_label_vertex(v);

      if(get_edge_attribute(e) != 'z')
	continue;

      if(!sanchor)
	continue;

      if(sanchor && sanchor[0] !='S')
	  continue;

      found++;
      if(found == 1)
      {
	write_item_html_file(fp);
	if(att)
	{
	  write_start_a_href_html_file(fp,la);
	  fprintf(fp,"%s",att);
	  write_break_html_file(fp);
	  fprintf(fp,"%s",la);
	  write_end_a_href_html_file(fp);
	}
	else
	{
	  write_start_a_href_html_file(fp,la);
	  fprintf(fp,"%s",la);
	  write_end_a_href_html_file(fp);
	}
	write_start_ul_html_file(fp);
      }	
      write_item_html_file(fp);
      write_start_pre_html_file(fp);
      write_string_html_file(fp,get_label_vertex(u));
      write_end_pre_html_file(fp);
    }
    if(found != 0)
	write_end_ul_html_file(fp);	
  }	
  write_end_ol_html_file(fp);
  write_end_body_html_file(fp);
  free(varray); varray = NULL;
}

